//
//  NSString+dateFormatter.m
//  GanJie
//
//  Created by 杨肖宇 on 2017/10/25.
//  Copyright © 2017年 yxy. All rights reserved.
//

#import "NSString+dateFormatter.h"

@implementation NSString (dateFormatter)

- (NSString *)convertStrToTime{
//    long long time= [self longLongValue];
//    NSDate * d = [[NSDate alloc]initWithTimeIntervalSince1970:time/1000.0];
//    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy"];
//    NSInteger cY = [[formatter stringFromDate:d] integerValue];
//    [formatter setDateFormat:@"MM"];
//    NSInteger cM = [[formatter stringFromDate:d] integerValue];
//    [formatter setDateFormat:@"dd"];
//    NSInteger cD = [[formatter stringFromDate:d] integerValue];
    
    NSArray * arr = [self componentsSeparatedByString:@"-"];
    NSInteger cY = [arr[0] integerValue];
    NSInteger cM = [arr[1] integerValue];
    NSArray * arr1 = [arr[2] componentsSeparatedByString:@" "];
    NSInteger cD = [arr1[0] integerValue];
    
    NSDate  *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    
    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];

    if (day == cD && year == cY && month == cM) {
        return @"今天";
    }
    if (day - cD == 1 && year == cY && month == cM) {
        return @"昨天";
    }
    
//    [formatter setDateFormat:@"yyyy-MM-dd"];
//    return [formatter stringFromDate:d];
    return [self componentsSeparatedByString:@" "][0];
}


/** 转换CST时间为 年月日时分秒 */
+ (NSString *)getDataByCSTTime:(NSString *)timeString
{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"EEE MMM dd HH:mm:ss 'CST' yyyy"];
    inputFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    NSDate *formatterDate = [inputFormatter dateFromString:timeString];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *newDateString = [outputFormatter stringFromDate:formatterDate];
    return newDateString;
}


@end
